﻿Imports System.IO
Public Class Assistant
    Private newFilenameDem As String = ""
    Public newFilenameTif As String = ""
    Public m_manager As LayerManager
    Public Sub AddImage(ByVal LayName As String, ByVal path As String, ByVal p1 As Drawing.PointF, _
                       ByVal p2 As Drawing.PointF, ByVal pl1 As Drawing.PointF, ByVal pl2 As Drawing.PointF, _
                       ByVal strGrid As String, ByVal strAxis As String, ByVal OffsetX As Double, ByVal OffsetY As Double)

        Dim utmP1 As New Drawing.PointF
        Dim utmP2 As New Drawing.PointF


        utmP1.X = pl1.X + OffsetX
        utmP1.Y = pl1.Y + OffsetY

        utmP2.X = pl2.X + OffsetX
        utmP2.Y = pl2.Y + OffsetY

        Dim sef As New IMGWorldFileLayer(LayName, path, p1, p2, utmP1, utmP2)
        m_manager.AddLayer(sef)
        m_manager.MoveLayerBottom(sef.GetLayerPos)

    End Sub
    Public Sub GetTypeManager(ByRef man As LayerManager)
        Me.m_manager = man
    End Sub
    ''' <summary>
    ''' 设置网格和网格对应的坐标
    ''' </summary>
    ''' <param name="strGrid">网格点的关键字</param>
    ''' <param name="LBPointX">左下角的相对坐标</param>
    ''' <param name="LBPointY">左下角的相对坐标</param>
    ''' <param name="XCells">网格数(网格点数-1)</param>
    ''' <param name="YCells">网格数(网格点数-1)</param>
    ''' <param name="Xunit">步长</param>
    ''' <param name="Yuint">步长</param>
    ''' <param name="LBUtmX">左下角的UTM坐标</param>
    ''' <param name="LBUtmY">左下角的UTM坐标</param>
    ''' <remarks></remarks>
    Public Sub SetGrid(ByVal strGrid As String, ByVal LBPointX As Double, ByVal LBPointY As Double _
                       , ByVal XCells As Integer, ByVal YCells As Integer _
                       , ByVal Xunit As Double, ByVal Yuint As Double, Optional ByVal LBUtmX As Double = 0, Optional ByVal LBUtmY As Double = 0)
        Dim grd As SDSGridLineLayer
        Me.m_manager.GetLayer(strGrid, grd)
        grd.SetGridProperty(LBUtmX, LBUtmY, XCells, YCells, Xunit, Yuint)
    End Sub
    ''' <summary>
    ''' 设置坐标轴和坐标轴对应的坐标
    ''' </summary>
    ''' <param name="strAxis">坐标轴的关键字</param>
    ''' <param name="LBPointX">左下角的相对坐标</param>
    ''' <param name="LBPointY">左下角的相对坐标</param>
    ''' <param name="XCells">网格数(网格点数-1)</param>
    ''' <param name="YCells">网格数(网格点数-1)</param>
    ''' <param name="Xunit">步长</param>
    ''' <param name="Yuint">步长</param>
    ''' <param name="LBUtmX">左下角的UTM坐标</param>
    ''' <param name="LBUtmY">左下角的UTM坐标</param>
    ''' <remarks></remarks>
    Public Sub SetAxis(ByVal strAxis As String, ByVal LBPointX As Double, ByVal LBPointY As Double _
                       , ByVal XCells As Integer, ByVal YCells As Integer _
                       , ByVal Xunit As Double, ByVal Yuint As Double, Optional ByVal LBUtmX As Double = 0, Optional ByVal LBUtmY As Double = 0)
        '坐标轴
        Dim axis As SDSAxisLayer
        Me.m_manager.GetLayer(strAxis, axis)
        axis.SetGridProperty(LBUtmX, LBUtmY, LBPointX, LBPointY, XCells, YCells, Xunit, Yuint)
    End Sub

    ''' <summary>
    ''' 根据主网格返回相对坐标
    ''' </summary>
    ''' <param name="UtmX">绝对坐标X</param>
    ''' <param name="UtmY">绝对坐标Y</param>
    ''' <param name="strGrid">主网格层对应的ID名称</param>
    ''' <param name="strAxis">坐标轴层对应的ID名称</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetRelativelyCoordinate(ByVal UtmX As Double, ByVal UtmY As Double, ByVal strGrid As String, ByVal strAxis As String) As Point3D
        Dim grd As SDSGridLineLayer
        Me.m_manager.GetLayer(strGrid, grd)
        '坐标轴
        Dim axis As SDSAxisLayer
        Me.m_manager.GetLayer(strAxis, axis)

        Dim RelativeC As New Point3D
        RelativeC.x = axis.LBPointXProperty + (UtmX - grd.LBPointXAProperty)
        RelativeC.y = axis.LBPointYProperty + (UtmY - grd.LBPointYAProperty)
        Return RelativeC
    End Function
    ''' <summary>
    ''' 根据相对坐标求得绝对坐标
    ''' </summary>
    ''' <param name="x">相对坐标X</param>
    ''' <param name="y">相对坐标Y</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetUtmFromRelative(ByVal x As Double, ByVal y As Double, ByVal strGrid As String, ByVal strAxis As String) As Point3D
        Dim grd As SDSGridLineLayer
        Me.m_manager.GetLayer(strGrid, grd)
        '坐标轴
        Dim axis As SDSAxisLayer
        Me.m_manager.GetLayer(strAxis, axis)

        Dim RelativeC As New Point3D
        RelativeC.x = grd.LBPointXAProperty + (x - axis.LBPointXProperty)
        RelativeC.y = grd.LBPointYAProperty + (y - axis.LBPointYProperty)
        Return RelativeC
    End Function

    ''' <summary>
    ''' DEM数据预处理
    ''' </summary>
    ''' <param name="FileName">DEM数据文件的名称，包含路径</param>
    ''' <param name="ApplicationPath">应用程序所在的文件夹，带\</param>
    ''' <param name="zone">DEM数据所在的区域</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function PreDealDem(ByVal FileName As String, ByVal ApplicationPath As String, ByVal zone As Integer) As String
        Dim FileName1 As String
        '添加文件列表
        If My.Computer.FileSystem.FileExists(FileName) = False Then
            Return False
        Else
            FileName1 = My.Computer.FileSystem.GetName(FileName)
            FileName1 = System.IO.Path.ChangeExtension(FileName1, "")
            If FileName1.EndsWith(".") Then
                FileName1 = FileName1.Substring(0, FileName1.Length - 1)
            End If
            Dim tempdemname As String = "temp" & FileName1 & ".dem"
            Dim tempTifname As String = "temp" & FileName1 & ".tif"
            If My.Computer.FileSystem.FileExists(ApplicationPath & "data\DemTemp\" & tempdemname) = True Then
                My.Computer.FileSystem.GetFileInfo(ApplicationPath & "data\DemTemp\" & tempdemname).Attributes = FileAttributes.Normal
                My.Computer.FileSystem.DeleteFile(ApplicationPath & "data\DemTemp\" & tempdemname)
            End If
            My.Computer.FileSystem.CopyFile(FileName, ApplicationPath & "data\DemTemp\" & tempdemname)
            If My.Computer.FileSystem.FileExists(ApplicationPath & "data\DemTemp\" & tempTifname) = True Then
                My.Computer.FileSystem.GetFileInfo(ApplicationPath & "data\DemTemp\" & tempTifname).Attributes = FileAttributes.Normal
                My.Computer.FileSystem.DeleteFile(ApplicationPath & "data\DemTemp\" & tempTifname)
            End If

            '把DEM数据转换成TIF格式
            Dim DosCommandOutput As New DosCommandOutput
            DosCommandOutput.CreateNoWindow = True
            Dim txtOut As String = DosCommandOutput.ExecuteTif(ApplicationPath & "data\", "gdal_translate.exe", " res\2.txt ", " -of GTiff DemTemp\" & tempdemname & " DemTemp\" & tempTifname)

            '转换DEM数据的坐标映射
            Dim oldGrid As New MapWinGIS.Grid
            oldGrid.Open(FileName)
            Dim oldProjection As String = oldGrid.Header.Projection
            If oldProjection Is Nothing Or oldProjection = "" Then
                oldProjection = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
            End If
            Dim selectedProjection As String = "+proj=utm +zone=" & zone & " +ellps=WGS84 +datum=WGS84 +units=m +no_defs"
            Dim newFilename As String = ApplicationPath & "data\DemTemp\" & "ReTemp_" & FileName1 & ".tif"
            If MapWinGeoProc.SpatialReference.ProjectGrid(oldProjection, selectedProjection, ApplicationPath & "data\DemTemp\" & tempTifname, newFilename, True) = False Then
                Return Nothing
            End If

            newFilenameDem = "ReTemp_" & FileName1 & ".dem"
            newFilenameTif = "ReTemp_" & FileName1 & ".tif"
            txtOut = DosCommandOutput.ExecuteTif(ApplicationPath & "data\", "gdal_translate.exe", "  res\2.txt ", " -of usgsdem DemTemp\" & newFilenameTif & " DemTemp\" & newFilenameDem)
        End If
        Return True
    End Function
    Public Sub AddTif(ByVal TifFileName As String)
        Dim FileName1 As String
        FileName1 = My.Computer.FileSystem.GetName(TifFileName)
        FileName1 = System.IO.Path.ChangeExtension(FileName1, "")
        If FileName1.EndsWith(".") Then
            FileName1 = FileName1.Substring(0, FileName1.Length - 1)
        End If
        Dim img As New IMGGEOFileLayer(FileName1, TifFileName)
        m_manager.AddLayer(img)
        img.UseTransParency = False
        m_manager.MoveLayerBottom(img.GetLayerPos())
    End Sub
    ''' <summary>
    ''' 返回用于三维显示的DEM名称 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetDemFileNameTo3D()
        Return newFilenameDem
    End Function
    ''' <summary>
    ''' 返回用于GIS显示的TIF文件名称
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetTifFileNameToGis()
        Return Me.newFilenameTif
    End Function

End Class
